﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Restaurant.DAO
{
    public class KhachHangDB
    {
        private SqlConnection con = KetNoiDB.TaoKetNoi();

        public DataTable LoadKhachHang()
        {
            con.Open();
            string strcmd = "dbo.spLoadKhachHang";
            SqlCommand cmd = new SqlCommand(strcmd, con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtb = new DataTable();
            adapt.Fill(dbtb);
            con.Close();
            return dbtb;
        }

        public bool ThemKhachHang(string tenKH, string cmnd, string sdt)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spThemKhachHang";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@tenKH", tenKH));
                cmd.Parameters.Add(new SqlParameter("@cmnd", cmnd));
                cmd.Parameters.Add(new SqlParameter("@sdt", sdt));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                con.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool XoaKhachHang(int ma)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spXoaKhachHang";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                con.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool SuaKhachHang(int maKH, string tenKH, string cmnd, string sdt)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spSuaKhachHang";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKH", maKH));
                cmd.Parameters.Add(new SqlParameter("@tenKH", tenKH));
                cmd.Parameters.Add(new SqlParameter("@cmnd", cmnd));
                cmd.Parameters.Add(new SqlParameter("@sdt", sdt));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                con.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public DataTable TimKhachHangID(int ma)
        {
            con.Open();
            string strcmd = "dbo.spTimKHID";
            SqlCommand cmd = new SqlCommand(strcmd, con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@ma", ma));
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtb = new DataTable();
            adapt.Fill(dbtb);
            con.Close();
            return dbtb;
        }

        public DataTable TimKhachHangTheoTen(string ten,string cmnd)
        {
            con.Open();
            string strcmd = "dbo.spTimKHTen";
            SqlCommand cmd = new SqlCommand(strcmd, con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@tenKH", ten));
            cmd.Parameters.Add(new SqlParameter("@cmnd", cmnd));
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtb = new DataTable();
            adapt.Fill(dbtb);
            con.Close();
            return dbtb;
        }

        public DataTable TimKHTheoBan(int maban)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spTimKHTheoBan";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maban", maban));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                con.Close();
                return dbtb;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
